package day13;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 题目:跳石板
 * https://www.nowcoder.com/practice/4284c8f466814870bae7799a07d49ec8?tpId=85&&tqId=29852&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] step = new int[m+1];   //多弄一个空间,让下标与数字对应
        //初始化值(最大)
        for (int i = 0; i < m+1; i++) {
            step[i] = Integer.MAX_VALUE;
        }
        step[n] = 0;
        for (int i = n; i < m; i++) {
            if(step[i] == Integer.MAX_VALUE) {   //即到不了这
                continue;
            }
            List<Integer> list = div(i);   //拿到约数
            for (int j:list) {
                if(i+j <= m && step[i] != Integer.MAX_VALUE) {
                    step[i+j] = Math.min(step[i+j],step[i]+1);
                } else if (i+j <= m){
                    step[i+j] = step[i]+1;
                }
            }
        }
        if(step[m] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(step[m]);
        }
    }

    //求约数
    public static List<Integer> div(int num) {
        List<Integer> list = new ArrayList<>();
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if(num % i == 0) {
                list.add(i);
                if(num / i != i) {
                    list.add(num/i);
                }
            }
        }
        return list;
    }


}
